<?php
require_once('Zend/Controller/Plugin/Abstract.php');

class Modules extends Zend_Controller_Plugin_Abstract
{
	public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
		$request = $this->getRequest();
		
 		$CONFIG = Zend_Registry::get('config');
 		$LANGUAGE = Zend_Registry::get('language');
 	 	$CACHE = Zend_Registry::get('cache');
 
		$main = Zend_Controller_Front::getInstance();
		
  
 		$module = $request->getModuleName();
		
		set_include_path(get_include_path().PATH_SEPARATOR."../app_mvc/$module/lib");
 
		if($CONFIG->application_state == 'production')
		{
			if (!$language_config = $CACHE->load("language_$module"))
			{	
				$modini = "../app_mvc/$module/config/language.ini";
				if (file_exists($modini))
				{
					$language_config = new Zend_Config_Ini($modini,'production',true);
					$LANGUAGE->merge($language_config);
					
					$CACHE->save($LANGUAGE, "language_$module");
				}
			}
			
		}
		else
		{
			$modini = "../app_mvc/$module/config/language.ini";
			if (file_exists($modini))
			{
				$language_config = new Zend_Config_Ini($modini,'production',true);
				$LANGUAGE->merge($language_config);
			}
		}

		 Zend_Registry::set('modules', $module);
		 Zend_Registry::set('config', $CONFIG);
		 Zend_Registry::set('language', $LANGUAGE);
    }
}
?>